package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.BaseConfigOperatBo;
import com.rla.domain.vo.BaseConfigOperatVo;
import com.rla.domain.BaseConfigOperat;
import com.rla.mapper.BaseConfigOperatMapper;
import com.rla.service.IBaseConfigOperatService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 参数配置——运营配置Service业务层处理
 *
 * @author rla
 * @date 2025-09-16
 */
@RequiredArgsConstructor
@Service
public class BaseConfigOperatServiceImpl implements IBaseConfigOperatService {

    private final BaseConfigOperatMapper baseMapper;

    /**
     * 查询参数配置——运营配置
     */
    @Override
    public BaseConfigOperatVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询参数配置——运营配置列表
     */
    @Override
    public TableDataInfo<BaseConfigOperatVo> queryPageList(BaseConfigOperatBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseConfigOperat> lqw = buildQueryWrapper(bo);
        Page<BaseConfigOperatVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询参数配置——运营配置列表
     */
    @Override
    public List<BaseConfigOperatVo> queryList(BaseConfigOperatBo bo) {
        LambdaQueryWrapper<BaseConfigOperat> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseConfigOperat> buildQueryWrapper(BaseConfigOperatBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseConfigOperat> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoGirl()), BaseConfigOperat::getPhotoGirl, bo.getPhotoGirl());
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoBoy()), BaseConfigOperat::getPhotoBoy, bo.getPhotoBoy());
        lqw.eq(bo.getIsSellOut() != null, BaseConfigOperat::getIsSellOut, bo.getIsSellOut());
        lqw.eq(StringUtils.isNotBlank(bo.getSellOutSign()), BaseConfigOperat::getSellOutSign, bo.getSellOutSign());
        lqw.eq(StringUtils.isNotBlank(bo.getAppletLogo()), BaseConfigOperat::getAppletLogo, bo.getAppletLogo());
        lqw.eq(StringUtils.isNotBlank(bo.getAboutUs()), BaseConfigOperat::getAboutUs, bo.getAboutUs());
        lqw.eq(bo.getFgcCreatedate() != null, BaseConfigOperat::getFgcCreatedate, bo.getFgcCreatedate());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcLastmodifier()), BaseConfigOperat::getFgcLastmodifier, bo.getFgcLastmodifier());
        lqw.eq(bo.getFgcLastmodifydate() != null, BaseConfigOperat::getFgcLastmodifydate, bo.getFgcLastmodifydate());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcCreator()), BaseConfigOperat::getFgcCreator, bo.getFgcCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcUpdatehelp()), BaseConfigOperat::getFgcUpdatehelp, bo.getFgcUpdatehelp());
        lqw.eq(bo.getFgcRowversion() != null, BaseConfigOperat::getFgcRowversion, bo.getFgcRowversion());
        return lqw;
    }

    /**
     * 新增参数配置——运营配置
     */
    @Override
    public Boolean insertByBo(BaseConfigOperatBo bo) {
        BaseConfigOperat add = BeanUtil.toBean(bo, BaseConfigOperat.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改参数配置——运营配置
     */
    @Override
    public Boolean updateByBo(BaseConfigOperatBo bo) {
        BaseConfigOperat update = BeanUtil.toBean(bo, BaseConfigOperat.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseConfigOperat entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除参数配置——运营配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
